/**
 * 
 */
package furniture.items3d;

import java.awt.Point;

import javax.vecmath.Vector3d;

import org.eclipse.swt.graphics.GC;

import furniture.items3d.bounds.Bounds3d;
import furniture.items3d.bounds.LayingBoundsVisitor;
import furniture.items3d.model.LayingModel;
import furniture.items3d.model.tessellation.ItemSet;
import furniture.items3d.model.tessellation.Tessellator;
import furniture.render3d.MatrixProjector;
import furniture.render3d.Projector;
import furniture.render3d.Renderer;
import furniture.render3d.TriangleSet;


/**
 * This class is used to generate a picture about the laying
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class LayingFigure {

	/**
	 * Simple projector to display items in 3d
	 */
	public static final Projector simpleProjector = 
		new MatrixProjector(new Vector3d(1.0,0.0,-0.4),new Vector3d(0.0,-1.0,0.4));

	/**
	 * Projector to display items in 2d
	 */
	public static final Projector flatProjector = 
		new MatrixProjector(new Vector3d(1.0,0.0,0),new Vector3d(0.0,-1.0,0));
	
	/**
	 * Projector to display items in a larger angle
	 */
	public static final Projector deepProjector =
		new MatrixProjector(new Vector3d(0.5,0.0,-0.5),new Vector3d(0.5,-0.5,0.5));
		
	
	/**
	 * Render an earth laying to an image of the given size
	 * @param earth
	 * @param size
	 * @param projector
	 * @return
	 */
	public static void draw(GC gc, LayingModel model ,Point size,Projector projector){
		
		//calculate "interesting" bounds in 3d space
		LayingBoundsVisitor boundsvisitor = new LayingBoundsVisitor();
		boundsvisitor.visit(model);
		Bounds3d bounds = boundsvisitor.bounds;
		
		//normalize bounds to be a cube
		Bounds3d cube = bounds.normalize();
		
		//turn model to a set of triangles
		Tessellator tessellator = new Tessellator(cube);
		TriangleSet triangles = tessellator.tessellate(model);
		
		//translate triangles to be in center
		Vector3d tr = new Vector3d(cube.min);
		tr.scale(-1);
		triangles.translate(tr);
		
		//add a box to the model to show bounds
//		TriangleSet cube1 = new Cube(false,cube.max.z-cube.min.z,new RGB(0, 0, 0),32);
		ItemSet items = new ItemSet();
		items.addSet(triangles);
		//items.addSet(cube1);
		
		//render image
		Renderer renderer = new Renderer(projector);
		renderer.render(gc, items, size);
	}
	
}
